package br.com.fatecpg.mamaeconomiza;

import org.json.JSONException;
import org.json.JSONObject;

import br.com.fatecpg.mamaeconomiza.library.ProductFunctions;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AdicionarProdutoActivity extends Activity {
	
	private EditText produtoNome;
	private EditText fabricante;
	private EditText preco;
	private EditText descricao;
	private EditText estabelecimento;
	private EditText endereco;
	private Button btnCadProd;
	
	private static String KEY_SUCCESS = "success";
	private static String KEY_SUCCESS_MSG = "success_msg";
	private static String KEY_ERROR = "error";
	private static String KEY_ERROR_MSG = "error_msg";
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adicionar_produto);
		
		produtoNome = (EditText) findViewById(R.id.editProdutoNome);
		fabricante = (EditText) findViewById(R.id.editFabricante);
		preco = (EditText) findViewById(R.id.editPreco);
		descricao = (EditText) findViewById(R.id.editDescricao);
		estabelecimento = (EditText) findViewById(R.id.editEstab);
		endereco = (EditText) findViewById(R.id.editEndereco);
		btnCadProd = (Button) findViewById(R.id.btnCadastrarProduto);
		
		btnCadProd.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				
				String name = produtoNome.getText().toString();
				String maker = fabricante.getText().toString();
				String price = preco.getText().toString();
				String description = descricao.getText().toString();
				String shop = estabelecimento.getText().toString();
				String address = endereco.getText().toString();
				
				ProductFunctions productFunctions = new ProductFunctions();
				JSONObject json = productFunctions.createProduct(name, maker, price, description, shop, address);
				
				try{
					if(json.getString(KEY_SUCCESS) != null){
						
						String res = json.getString(KEY_SUCCESS);
						
						if(Integer.parseInt(res) == 1){
							Toast.makeText(getApplicationContext(), json.getString(KEY_SUCCESS_MSG), Toast.LENGTH_LONG).show();
							
							Intent intent = new Intent(getApplicationContext(), MainActivity.class);
							startActivity(intent);
							
						} else {
							Toast.makeText(getApplicationContext(), json.getString(KEY_ERROR_MSG), Toast.LENGTH_LONG).show();
						}
					}
					
				}catch(JSONException e){
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.adicionar_produto, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
